package com.demo.mqtt.picker.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.mqtt.common.client.MqttConnectionUtils;
import com.demo.mqtt.common.fromCommon.dto.SplitPageDTO;
import com.demo.mqtt.common.fromResource.entity.SysUser;
import com.demo.mqtt.picker.entity.PickerInfo;
import com.demo.mqtt.picker.entity.PickerInfoRelPoint;
import com.demo.mqtt.picker.entity.dto.PickerInfoDTO;
import com.demo.mqtt.picker.entity.vo.PickerInfoVO;
import com.demo.mqtt.picker.mapper.PickerInfoMapper;
import com.demo.mqtt.picker.service.IPickerInfoRelPointService;
import com.demo.mqtt.picker.service.IPickerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;

/**
 * @program: mqtt
 * @description: 数采器实现类
 * @author: liuYunKai
 * @create: 2021-03-30 15:23
 */
@Service
public class PickerInfoServiceImpl extends ServiceImpl<PickerInfoMapper, PickerInfo> implements IPickerInfoService {
    @Autowired
    private IPickerInfoRelPointService pickerInfoRelPointService;
    @Autowired
    private MqttConnectionUtils mqttConnectionUtils;

    @Override
    public IPage<PickerInfoVO> getPickinfoPage(SplitPageDTO splitPageDTO, PickerInfoDTO pickerInfoDTO, SysUser sysUser) {

        List<PickerInfoVO> dasConfigDTOIPage = baseMapper.selectPickerInfoList(pickerInfoDTO);

        IPage<PickerInfoVO> pages = new Page<>();
        pages.setRecords(dasConfigDTOIPage);
        pages.setCurrent(splitPageDTO.getPage());
        pages.setSize(splitPageDTO.getPageSize());
        return pages;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void pickerSave(PickerInfoDTO pickerInfoDTO) {

        PickerInfo pickerInfo = pickerInfoDTO.convert(PickerInfo.class);

        if (save(pickerInfo)) {

            List<PickerInfoRelPoint> pickerInfoRelPointList = transRelPoints(pickerInfoDTO.getPickerInfoRelPointList(), pickerInfo.getId());

            if (CollectionUtils.isEmpty(pickerInfoRelPointList)) {
                pickerInfoRelPointService.saveBatch(pickerInfoRelPointList);
            }

            //下发配置
            mqttConnectionUtils.sendSensorSetting(pickerInfo);

        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByDTO(String id, PickerInfoDTO pickerInfoDTO) {
        PickerInfo pickerInfo = pickerInfoDTO.convert(PickerInfo.class);
        pickerInfo.setId(id);

        if (updateById(pickerInfo)) {
            List<PickerInfoRelPoint> pickerInfoRelPointList = transRelPoints(pickerInfoDTO.getPickerInfoRelPointList(), pickerInfo.getId());

            if (CollectionUtils.isEmpty(pickerInfoRelPointList)) {
                pickerInfoRelPointService.saveOrUpdateBatch(pickerInfoRelPointList);
            }

            //下发配置
            mqttConnectionUtils.sendSensorSetting(pickerInfo);
        }


    }

    private List<PickerInfoRelPoint> transRelPoints(List<PickerInfoRelPoint> pickerInfoRelPointList, String pickerId) {

        for (PickerInfoRelPoint point : pickerInfoRelPointList) {
            point.setPickerId(pickerId);
        }

        return pickerInfoRelPointList;
    }


}
